/**
 * 编辑-行情走势js
 */
$(function() {
	oneCategory_select(0);
});

function oneCategory_select(parentId){
	$.ajax({
        type: 'post',
        url: '../../sys/category/selectParentId',
        data: {
        	parentId : parentId,
        	type : 0
        },
        async: false,
        success: function(data) {
        	var result = "";
        	for(var i = 0; i < data.length; i++){
        		result += "<option value='"+data[i].categoryId+"'>"+data[i].name+"</option>";
        	}
        	$("#oneCategory").append(result);
        	
        	if (parentId == 0) {
        		twoCategory_select(data[0].categoryId);
        	}else {
        		twoCategory_select(parentId);
        	}
        }
	})
}

function twoCategory_select(parentId){
	if (parentId != 0) {
		$.ajax({
	        type: 'post',
	        url: '../../sys/category/selectParentId',
	        data: {
	        	parentId : parentId,
	        	type : 1,
	        },
	        async: false,
	        success: function(data) {
	        	var result2 = "";
	        	for(var i = 0; i < data.length; i++){
	        		result2 += "<option value='"+data[i].categoryId+"'>"+data[i].name+"</option>";
	        	}
	        	$("#twoCategory").html(result2);
	        }
		})
	}
}

function listFactory(categoryId,categoryTwo){
	var factoryName;
	$.ajax({
        type: 'post',
        url: '../../sys/factory/listFactory',
        data: {
        	categoryId : categoryId,
        	categoryTwo : categoryTwo,
        	name : vm.quotationtrend.selectName,
        },
        async: false,
        success: function(data) {
        	var result = "";
        	result += "<option value=''>请选择</option>";
        	for(var i = 0; i < data.length; i++){
        		result += "<option value='"+data[i].id+"'>"+data[i].name+"</option>";
        	}
        	factoryName = data;
        	$("#factory").html(result);
        }
	})
	return factoryName;
}

var vm = new Vue({
	el:'#dpLTE',
	data: {
		contentStr : "",
		factoryName:"",
		quotationtrend: {
			content : "",
			categoryId : "",
			categoryTwo : "",
			factoryId : "",
			selectName:"",
		}
	},
	methods : {
		setForm: function() {
			$.SetForm({
				url: '../../sys/quotationtrend/info?_' + $.now(),
		    	param: vm.quotationtrend.id,
		    	async: false,
		    	success: function(data) {
		    		vm.quotationtrend = data;
		    		 var editor = UE.getEditor('editor');
		    		    editor.ready(function() {
		    		    	if(vm.quotationtrend.content !=null && vm.quotationtrend.content!=""){
		    		    		editor.setContent(vm.quotationtrend.content); 
		    		    	}
		    			});
		    		    editor.addListener("contentchange", function () {
		    		        $("#introdution").html(editor.getContent());
		    		        vm.contentStr = $("#introdution").html(editor.getContent()).html();
		    		    })
		    		    
		    		    //一级分类编辑给select 赋值
			    		$("#oneCategory option[value="+vm.quotationtrend.categoryId+"]").attr("selected", "selected");
		    		    twoCategory_select(vm.quotationtrend.categoryId);
		    		    //二级分类赋值
		    		    $("#twoCategory option[value="+vm.quotationtrend.categoryTwo+"]").attr("selected", "selected");
		    		    //厂名
		    		    listFactory(vm.quotationtrend.categoryId,vm.quotationtrend.categoryTwo);
		    		    //厂名赋值
		    		    $("#factory option[value="+vm.quotationtrend.factoryId+"]").attr("selected", "selected");
		    		    vm.quotationtrend.name=data.factoryName;
		    		    vm.quotationtrend.factoryId =data.factoryId;
		    	}
			});
		},
		//一级分类获取select选中值
		oneCategory_select:function(){
			var parentId = $('#oneCategory option:selected').val();
			vm.quotationtrend.categoryId = parentId;
			oneCategory_select(parentId);
		},
		//二级分类获取select选中值
		twoCategory_select:function(){
			vm.quotationtrend.categoryTwo = $('#twoCategory option:selected').val();
			var categoryId = vm.quotationtrend.categoryId;
			var categoryTwo = vm.quotationtrend.categoryTwo;
			listFactory(categoryId,categoryTwo);
			vm.factoryName = listFactory(categoryId,categoryTwo);
			$("#factoryList").show();
		},
		//厂名获取select选中值
//		factory_select:function(){
//			vm.quotationtrend.factoryId = $('#factory option:selected').val();
//		},
		factory_select:function(event){
			vm.quotationtrend.factoryId = event.target.getAttribute("id");
			vm.quotationtrend.name = event.target.getAttribute("value");
			$("#factoryList").hide();
		},
		factory_show:function(){
			$("#factoryList").toggle();
		},
		//模糊搜索
		search:function (event) {
			vm.quotationtrend.selectName = vm.quotationtrend.selectName;
			vm.factoryName = listFactory(vm.quotationtrend.categoryId,vm.quotationtrend.categoryTwo);
			$("#factoryList").show();
		},
		acceptClick: function() {
			if (!$('#form').Validform()) {
		        return false;
		    }
			vm.quotationtrend.content = "";
			vm.quotationtrend.content = vm.contentStr;
		    $.ConfirmForm({
		    	url: '../../sys/quotationtrend/update?_' + $.now(),
		    	param: vm.quotationtrend,
		    	success: function(data) {
		    		$.currentIframe().vm.load();
		    	}
		    });
		}
	}
})

